package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO getTurnoverStatistic(LocalDate begin, LocalDate end) {
        //存放begin到end的日期
        List<LocalDate> dateList = new ArrayList<>();
        end = end.plusDays(1);
        while(!begin.equals(end)){
            //计算指定日期后面的时间
            dateList.add(begin);
            begin = begin.plusDays(1);
        }
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate dateTime : dateList) {

            //通过日期找到orders然后将金额相加
            LocalDateTime beginDate = LocalDateTime.of(dateTime, LocalTime.MIN);
            LocalDateTime endDate = LocalDateTime.of(dateTime,LocalTime.MAX);
            //select sum(amount) from orders where order_time > ? and order_time <? and status = 5;
            Map map = new HashMap();
            map.put("begin",beginDate);
            map.put("end",endDate);
            map.put("status", Orders.COMPLETED  );
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }

        //封装返回结果
        return TurnoverReportVO
                .builder()
                .turnoverList(StringUtils.join(turnoverList, ","))
                .dateList(StringUtils.join(dateList,","))
                .build();
    }


    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //存放begin到end的日期
        List<LocalDate> dateList = new ArrayList<>();
        end = end.plusDays(1);
        while(!begin.equals(end)){
            //计算指定日期后面的时间
            dateList.add(begin);
            begin = begin.plusDays(1);
        }
        List<Integer> newUserList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();

        //select count(id) from user where create_time
        for (LocalDate dateTime : dateList) {
            LocalDateTime beginDate = LocalDateTime.of(dateTime, LocalTime.MIN);
            LocalDateTime endDate = LocalDateTime.of(dateTime,LocalTime.MAX);
            LocalDateTime zDate = LocalDateTime.of(1970, 1, 1, 0, 0);
            //直接查用户数量
            Map map = new HashMap();
            Map map1 = new HashMap();
            map.put("begin",beginDate);
            map.put("end", endDate);
            map1.put("begin", zDate);
            map1.put("end",endDate);
            Integer newUser = userMapper.countByMap(map);
            Integer totalUser = userMapper.countByMap(map1);
            newUserList.add(newUser);
            totalUserList.add(totalUser);
        }


        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();
    }
}








